package com.tianqu.tms.www.car.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.CommercialVehicle;
import com.tianqu.tms.entity.Vendor;
import com.tianqu.tms.www.car.controller.CarLicensePlateValidateController;
import com.tianqu.tms.www.car.viewhelper.CarCreationInputViewHelper;

/**
 * @author zhangxueliang
 * 车牌号码验证是否存在
 */
public class CarLicensePlateValidateAction extends TmsAction{
	private Logger logger = Logger.getLogger(CarLicensePlateValidateAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");	
 
		//
		CarCreationInputViewHelper carCreationInputViewHelper = new CarCreationInputViewHelper();
		carCreationInputViewHelper = (CarCreationInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		if(carCreationInputViewHelper.getIsImageBack() == 1){//上传图片返回
			carCreationInputViewHelper.setIsImageBack(0);
			request.setAttribute(TmsConstants.VIEWHELP, carCreationInputViewHelper);
			return mapping.getInputForward();
		}
		//
		CarLicensePlateValidateController controller = new CarLicensePlateValidateController();
		controller.execute(uc);
		carCreationInputViewHelper = new CarCreationInputViewHelper();
		carCreationInputViewHelper = (CarCreationInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		CommercialVehicle commercialVehicle = CommercialVehicle.getInstance();
		// 车辆品牌
		Vendor vendor = Vendor.getInstance();
		if(!StringUtil.isNullOrBlank(carCreationInputViewHelper.getVendorId())){
			if("V".equals(carCreationInputViewHelper.getVendorId().substring(0,1))){
				carCreationInputViewHelper.setFromType(2);
				carCreationInputViewHelper.setVendorList(commercialVehicle.findAllVendorInfo());
			}else if("S".equals(carCreationInputViewHelper.getVendorId().substring(0,1))){
				carCreationInputViewHelper.setFromType(1);
				carCreationInputViewHelper
				.setVendorList(vendor.findAllVendorInfo());
			}
		}
			
			
		if(StringUtil.isNullOrBlank(carCreationInputViewHelper.getSerial())){
			//打出提示信息   该车辆数据库不存在！
			setErrorsToRequest(request, TmsConstants.IA005 ,null);
			request.setAttribute(TmsConstants.VIEWHELP, carCreationInputViewHelper);
			return mapping.getInputForward();
		}
		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		request.setAttribute(TmsConstants.VIEWHELP, carCreationInputViewHelper);
		return mapping.findForward(TmsConstants.SUCCESS);
	}

}
